package com.neiquan.neiquandemo.http;


import com.alibaba.fastjson.JSON;
import com.neiquan.neiquandemo.appliction.MyApplication;
import com.neiquan.neiquandemo.entity.User;

import net.neiquan.okhttp.NetCallBack;
import net.neiquan.okhttp.OkHttpUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 作者 ： dyj
 * 时间 ： 2016/2/21.
 */

public class NetUtils {

    /**
     * pageSize
     */
    public static int PAGE_SIZE = 10;

    private static NetUtils single = null;

    // 静态工厂方法
    public static NetUtils getInstance() {
        if (single == null) {
            single = new NetUtils();
        }
        return single;
    }

    public void norPost(String url, Object object, final NetCallBack callback, final Class classType) {
        OkHttpUtils.getInstance().postJsonAnsy(url, JSON.toJSONString(object), callback, classType);
    }

    public void norPost(String url, Map<String, Object> map, final NetCallBack callback, final Class classType) {
        if (map == null) {
            map = new HashMap<String, Object>();
        }
        User user = MyApplication.getInstance().user;
        if (user != null) {
            map.put("userid",user.getUser().getId());
            map.put("token",user.getToken());
        }
        // map.put("deviceType", 0+""); // 拼接userid
        OkHttpUtils.getInstance().postJsonAnsy(url, map, callback, classType);
    }

    public void norPostUser(String url, Map<String, Object> map, final NetCallBack callback, final Class classType) {
        if (map == null) {
            map = new HashMap<String, Object>();
        }
        if (MyApplication.getInstance().isLogin()) {
            map.put("token", MyApplication.getInstance().user.getToken()); // 拼接userid
        }
        //map.put("deviceType", 0); // 拼接userid
        OkHttpUtils.getInstance().postJsonAnsy(url, map, callback, classType);
        //OkHttpUtils.getInstance().SetSessionOutCls(WelcomeActivity.class);
    }

    /**
     * 登录
     * @param phone
     * @param password(不小于6位,不大于20位)
     * @param equi_type（登录设备类型0 Android,1 IOS,2 微信）
     * @param callback
     * @param classType
     */
    public void login(String phone, String password, String equi_type, String registId, final NetCallBack callback, final Class classType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("phone", phone);
        params.put("password", password);
        params.put("equi_type", equi_type);
        params.put("registId", registId);
        norPost(APPURL.LOGIN, params, callback, classType);
    }
}
